package com.sso.login.controller;

import com.sso.login.pojo.User;
import com.sso.login.servie.LoginService;
import com.sso.login.utils.LoginCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author Rocky
 * @create 2023-04-01 20:34
 */
@Controller
@RequestMapping("/login" )
public class LoginController {
    @Autowired
    private LoginService loginService;

    @PostMapping
    public String doLogin(User user, HttpSession session, HttpServletResponse response){
        String target = (String) session.getAttribute("target");

        String userName = user.getUserName();
        String userPwd = user.getUserPwd();

        User user1=loginService.queryUser(userName,userPwd);
        if (user1 != null){
            String token = UUID.randomUUID().toString();
            Cookie cookie = new Cookie("TOKEN", token);
            cookie.setDomain("zkjshop.com");
            response.addCookie(cookie);
            LoginCache.loginUser.put(token,user1);
            //重定向到target地址
            return "redirect:" + target;
        }else {
            session.setAttribute("msg","用户名或密码输入错误请重新输入");
            return "login";
        }

    }




    @GetMapping("info")
    @ResponseBody
    public ResponseEntity<User> getUserInfo(String token){
        if (!StringUtils.isEmpty(token)){
            User user = LoginCache.loginUser.get(token);
            return ResponseEntity.ok(user);
        }else {
            return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
        }
    }

    @GetMapping("/out")
    @ResponseBody
    protected void out(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession(false); // 获取当前请求的session，如果没有session则返回null
        if (session != null) {
            session.invalidate(); // 使session失效
        }
        Cookie[] cookies = request.getCookies(); // 获取当前请求的所有cookie
        if (cookies != null) {
            for (Cookie cookie : cookies) {

                if (cookie.getName().equals("JSESSIONID") || cookie.getName().equals("TOKEN") || cookie.getName().equals("token")) { // 如果存在名为"sessionId"的cookie，则将其值设置为空并将其过期时间设置为0
                    LoginCache.loginUser.remove(cookie.getValue());   //清除登陆记录
                    cookie.setValue("");
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                }
            }
        }

        HttpSession session1 = request.getSession();

        response.sendRedirect("http://login.zkjshop.com:9000/view/login?target=http://www.zkjshop.com:9010/view/index"); // 重定向到登录页面
    }

}
